package com.linkai.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linkai.dto.AdminBproductExcelDto;
import com.linkai.entity.Img;
import com.linkai.entity.PushImg;
import com.linkai.listener.BackProductDataListener;
import com.linkai.service.BproductService;
import com.linkai.service.FileImgService;
import com.linkai.service.ImgService;
import com.linkai.service.PushImgService;
import com.linkai.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yamon
 * @Date 2020-09-11 20:37
 * @Description
 * @Version 1.0
 */
@RequestMapping("/admin/file")
@Controller
public class FileImgController extends BaseController {
    @Autowired
    private FileImgService fileImgService;
    @Autowired
    private ImgService imgService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private PushImgService pushImgService;

    @Autowired
    private BproductService bproductService;

    @RequestMapping(value = "/img/upload", method = RequestMethod.POST)
    @ResponseBody
    public String uploadProductImg(@RequestParam(value = "file") MultipartFile file) {
        try {
            //先获取proId
            final String proId = stringRedisTemplate.opsForValue().get("proId");
            if (proId != null) {
                //将文件信息上传到数据库
                //先删除
                final QueryWrapper<Img> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("pro_id", proId);
                final boolean remove = imgService.remove(queryWrapper);
                //添加
                final Map<String, String> fileUrlInfo = FileUploadUtils.upload(file);
                fileImgService.save2Db(file, Integer.parseInt(proId), fileUrlInfo);
                //最后清空proId
                stringRedisTemplate.delete("proId");
            } else {
                System.out.println("proId is null!");
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "upload";
    }

    /**
     * 推新历史记录中的添加图片
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/pushNew/upload", method = RequestMethod.POST)
    @ResponseBody
    public String uploadPushNewUploader(@RequestParam(value = "file") MultipartFile file) {
        try {
            //先获取proId
            final String pushId = stringRedisTemplate.opsForValue().get("pushId");
            if (pushId != null) {
                //将文件信息上传到数据库
                //添加
                final Map<String, String> fileUrlInfo = FileUploadUtils.upload(file);
                int flag = fileImgService.save2Db_pushNew(file, fileUrlInfo);
                //最后清空pushId
                stringRedisTemplate.delete("pushId");
                //保存成功后，给两个表里保存信息
                if (flag != 0) {
                    final PushImg entity = new PushImg();
                    entity.setImgId(flag);
                    entity.setPushId(Integer.valueOf(pushId));
                    pushImgService.save(entity);
                }
            } else {
                System.out.println("pushId is null!");
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "upload";
    }

    /**
     * 推广历史中的更新文件
     *
     * @param file file
     * @return flag
     */
    @RequestMapping(value = "/pushNew/update", method = RequestMethod.POST)
    @ResponseBody
    public String updatePushNewUploader(@RequestParam(value = "file") MultipartFile file) {
        try {
            //先获取pushId
            final String pushId = stringRedisTemplate.opsForValue().get("pushId");
            if (pushId != null) {
                //查找对应的imgId，先删除后添加
                final List<Img> imgsByPushId = pushImgService.getImgsByPushId(Integer.valueOf(pushId));
                if (imgsByPushId != null) {
                    imgsByPushId.forEach(img -> {
                        if (img != null) {
                            fileImgService.deleteByImgId(img.getImgId());
                        }
                    });
                }

                final QueryWrapper<PushImg> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("push_id", pushId);
                pushImgService.remove(queryWrapper);

                //添加
                final Map<String, String> fileUrlInfo = FileUploadUtils.upload(file);
                int flag = fileImgService.save2Db(file, Integer.parseInt(pushId), fileUrlInfo);
                //最后清空pushId
                stringRedisTemplate.delete("pushId");
                //保存成功后，给两个表里保存信息
                if (flag != 0) {
                    final PushImg entity = new PushImg();
                    entity.setImgId(flag);
                    entity.setPushId(Integer.valueOf(pushId));
                    pushImgService.save(entity);
                }
            } else {
                System.out.println("pushId is null!");
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "upload";
    }

    /**
     * 基本资料中的商品的模板上传
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/bproduct/upload", method = RequestMethod.POST)
    @ResponseBody
    public int uploadBackProductInfoUploader(@RequestParam(value = "file") MultipartFile file) {
        try {
            final HashMap<String, Object> columnMap = new HashMap<>();
            columnMap.put("pro_id",-1);
            imgService.removeByMap(columnMap);
            Map<String, String> fileUrlInfo  = FileUploadUtils.upload(file);
            return fileImgService.save2Db(file,-1,fileUrlInfo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 下载一个样例
     * @param request 请求
     * @param response  响应
     * @return 异常
     * @throws Exception 全局异常
     */
    @RequestMapping(value = "/bproduct/download",method = RequestMethod.GET)
    @ResponseBody
    public String exportSelectedSimpleCases(HttpServletRequest request, HttpServletResponse response) throws Exception{

        String filePath= null;
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        filePath = fileImgService.downloadTemplate(request,response);
        return filePath;
    }


    /**
     * 基本资料中的商品的模板上传
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/bproduct/uploadCvs", method = RequestMethod.POST)
    @ResponseBody
    public int uploadBackProductTemplate(@RequestParam(value = "file") MultipartFile file) {
        BackProductDataListener backProductDataListener = new BackProductDataListener(bproductService);
        try {
            EasyExcel.read(file.getInputStream(), AdminBproductExcelDto.class, backProductDataListener).sheet().doRead();
            return 1;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
